﻿using System;
using JY.Wpf.Core.Enums;
using JY.Wpf.Core.Tools;

namespace JY.Wpf.Core.Models
{
    /// <summary>
    /// 服务模型
    /// </summary>
    public class License:ModelBase
    {
        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="licenseExpiredDate">授权日期</param>
        /// <param name="serviceExpiredDate">服务日期</param>
        public License(DateTime licenseExpiredDate, DateTime serviceExpiredDate)
        {
            this.LicenseExpiredDate = licenseExpiredDate;
            this.ServiceExpiredDate = serviceExpiredDate;
        }
        /// <summary>
        /// 构造器
        /// </summary>
        public License()
        {
        }
        #endregion

        #region # 属性
        #region 企业名称——string EnterpriseName
        private string _enterpriseName;

        /// <summary>
        /// 企业名称
        /// </summary>
        public string EnterpriseName
        {
            get { return this._enterpriseName;}
            set { this._enterpriseName = value;base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 唯一码——string UniqueCode
        private string _uniqueCode;
        /// <summary>
        /// 唯一码
        /// </summary>
        public string UniqueCode
        {
            get { return this._uniqueCode; }
            set { this._uniqueCode = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 授权过期日期—— DateTime LicenseExpiredDate
        private DateTime _licenseExpiredDate;
        /// <summary>
        /// 授权过期日期
        /// </summary>
        public DateTime LicenseExpiredDate
        {
            get { return this._licenseExpiredDate; }
            set
            {
                this._licenseExpiredDate = value;
                base.NotifyOfPropertyChange();
                this.CheckServer();
            }
        }
        #endregion

        #region 服务过期日期 —— DateTime ServiceExpiredDate
        private DateTime _serviceExpiredDate;

        /// <summary>
        /// 服务过期日期
        /// </summary>
        public DateTime ServiceExpiredDate
        {
            get { return this._serviceExpiredDate; }
            set
            {
                this._serviceExpiredDate = value;
                base.NotifyOfPropertyChange();
                this.CheckServer();
            }
        }

        #endregion

        #region 服务过期提示消息——string ServiceExpiredMessage
        private string _serviceExpiredMessage;
        /// <summary>
        /// 服务过期提示消息
        /// </summary>
        public string ServiceExpiredMessage
        {
            get { return this._serviceExpiredMessage; }
            set { this._serviceExpiredMessage = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 授权状态——ServiceStatus ServiceStatus
        private ServiceStatus _serviceStatus;
        /// <summary>
        /// 授权状态
        /// </summary>
        public ServiceStatus ServiceStatus
        {
            get { return this._serviceStatus; }
            set { this._serviceStatus = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 授权状态——ServiceStatus LicenseStatus
        private ServiceStatus _licenseStatus;
        /// <summary>
        /// 授权状态
        /// </summary>
        public ServiceStatus LicenseStatus
        {
            get { return this._licenseStatus; }
            set { this._licenseStatus = value; base.NotifyOfPropertyChange(); }
        }
        #endregion
        #endregion

        #region # 方法
        #region # 检测服务是否过期——void CheckServer()
        /// <summary>
        /// 检测服务是否过期
        /// </summary>
        private void CheckServer()
        {
            TimeSpan serviceDate = (this.ServiceExpiredDate - DateTime.Today);
            TimeSpan licenseDate = (this.LicenseExpiredDate - DateTime.Today);

            if (DateTime.Today > this.ServiceExpiredDate)
            {
                this.ServiceExpiredMessage = $"您的系统服务授权已过期[{serviceDate.Days}]天！请及时缴纳服务费用，不然会影响到对您的技术支持，严重时会影响系统使用呦。立高售后服务电话：13314512466。";
                this.ServiceStatus = ServiceStatus.已过期;
            }
            else if (Math.Abs(serviceDate.Days) <= 15 && Math.Abs(serviceDate.Days) >= 0)
            {
                this.ServiceExpiredMessage = $"您的系统服务授权还有[{serviceDate.Days}天]到期，请及时缴纳服务费用，不然会影响到对您的技术支持，严重时会影响系统使用呦。立高售后服务电话：13314512466。";
                this.ServiceStatus = ServiceStatus.即将过期;
            }
            else
            {
                this.ServiceExpiredMessage = "";
                this.ServiceStatus = ServiceStatus.未过期;
            }

            if (DateTime.Today > this.LicenseExpiredDate)
            {
                this.ServiceExpiredMessage = $"您的系统使用已过期[{licenseDate.Days}]天！请及时缴纳相关费用获取新的授权。立高售后服务电话：13314512466。";
                this.LicenseStatus = ServiceStatus.已过期;
            }
            else if (Math.Abs(licenseDate.Days) <= 15 && Math.Abs(licenseDate.Days) >= 0)
            {
                this.ServiceExpiredMessage = $"您的系统使用授权还有[{licenseDate.Days}天]到期，请及时缴纳相关费用，不然会影响到对您的技术支持，严重时可能会影响系统使用呦。立高售后服务电话：13314512466。";
                this.LicenseStatus = ServiceStatus.即将过期;
            }
        }
        #endregion
        #endregion
    }
}
